package com.example.exercise.sys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.sys.vo.SysAdminVO;
import com.example.exercise.sys.dao.entity.SysAdminEntity;
import com.example.exercise.sys.dao.SysAdminDAO;

/**
* 
*/
@Service
public class SysAdminService {

    @Autowired
    private SysAdminDAO sysAdminDAO;

    public List<SysAdminVO> findByWhere(Page<?> page, SysAdminVO sysAdminVO){
        List<SysAdminVO> list = new ArrayList<>();
        List<SysAdminEntity> list0 = sysAdminDAO.findByWhere(page, sysAdminVO);
        for (SysAdminEntity sysAdminEntity : list0) {
            list.add(toSysAdminVO(sysAdminEntity));
        }
        return list;
    }

    public SysAdminVO findByKey(SysAdminVO sysAdminVO){
        SysAdminEntity sysAdminEntity = sysAdminDAO.findByKey(sysAdminVO);
        return toSysAdminVO(sysAdminEntity);
    }

    public int insert(SysAdminVO sysAdminVO){
        return sysAdminDAO.insert(toSysAdminEntity(sysAdminVO));
    }

    public int updateByKey(SysAdminVO sysAdminVO){
        return sysAdminDAO.updateByKey(toSysAdminEntity(sysAdminVO));
    }

    public int deleteByKey(SysAdminVO sysAdminVO){
        return sysAdminDAO.deleteByKey(sysAdminVO);
    }

    private SysAdminVO toSysAdminVO(SysAdminEntity sysAdminEntity) {
        SysAdminVO sysAdminVO = new SysAdminVO();
                sysAdminVO.setUserId(String.valueOf(sysAdminEntity.getUserId()));
        sysAdminVO.setUserName(sysAdminEntity.getUserName());
        sysAdminVO.setType(String.valueOf(sysAdminEntity.getType()));
        sysAdminVO.setReason(sysAdminEntity.getReason());

        return sysAdminVO;
    }

    private SysAdminEntity toSysAdminEntity(SysAdminVO sysAdminVO) {
        SysAdminEntity sysAdminEntity = new SysAdminEntity();
                String userId = sysAdminVO.getUserId();
        if(userId != null) {
            sysAdminEntity.setUserId(Integer.parseInt(sysAdminVO.getUserId()));
        }
        sysAdminEntity.setUserName(sysAdminVO.getUserName());
        String type = sysAdminVO.getType();
        if(type != null) {
            sysAdminEntity.setType(Integer.parseInt(sysAdminVO.getType()));
        }
        sysAdminEntity.setReason(sysAdminVO.getReason());

        return sysAdminEntity;
    }

}